<?php
declare (strict_types = 1);

namespace app\auth\model;

/**
 * @mixin \think\Model
 */
class RoleModel extends BaseModel
{
    protected $table = 'tb_roles';
    protected $append = ['type_str','status_str','range_str'];

    public function getCreateTimeAttr($value)
    {
        if(empty($value)) return 0;
        return date('Y-m-d H:i:s', $value);
    }

    const WITH_DEPT = 'dept';
    const WITH_RIGHTS = 'rights';
    public function rights()
    {
        return $this->hasMany('app\auth\model\RoleRightLinkModel', 'role_id');
    }

    public function dept()
    {
        return $this->belongsTo('app\auth\model\DeptModel', 'dept_id');
    }

    //角色类型
    const TYPE_ADMIN = 1;
    const TYPE_OTHER = 2;
    static $TYPE_STR = [
        self::TYPE_ADMIN => '管理员',
        self::TYPE_OTHER => '其他',
    ];
    public function getTypeStrAttr($value,$data){
        return @self::$TYPE_STR[$data['type']];
    }

    //角色状态
    const STATUS_NORMAL = 1;
    const STATUS_CLOSE = 2;
    static $STATUS_STR = [
        self::STATUS_NORMAL => '正常',
        self::STATUS_CLOSE => '冻结'
    ];
    public function getStatusStrAttr($value,$data){
        return @self::$STATUS_STR[$data['status']];
    }

    //查看范围
    const RANGE_SELF = 'self';
    const RANGE_ANY = 'any';
    static $RANGE_STR = [
        self::RANGE_SELF => '自己',
        self::RANGE_ANY => '任何'
    ];
    public function getRangeStrAttr($value,$data){
        return @self::$RANGE_STR[$data['range']];
    }
}
